package com.red.star.macalline.data.mappers;

import com.red.star.macalline.data.core.base.MyMapper;
import com.red.star.macalline.data.entity.Group;
import com.red.star.macalline.data.entity.StoredCard;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/2/7.
 */
public interface GroupMapper extends MyMapper<Group> {

    Group findGroupById(Integer id);

    Group findGroupByIdAndStatus0(Integer id);

    List<Group> findGroupList(Group group);

    List<Group> findAllGroupList(Group group);

    void insertGroupFans(@Param("groupId") Integer groupId, @Param("bFansId") Integer bFansId);

    List getGroupFansByGroupId(Integer groupId);

    void updateGroupFansBygroupId(@Param("groupId") Integer groupId, @Param("bFansId") Integer bFansId);

    void updateStatusThreeById(Integer id);

    List<Group> getAllList();

    Group findOne(Integer id);

    void updateStatusById(@Param("groupId") Integer groupId, @Param("status") Integer status);

    Integer insertReturnId(Group group);

    List<Group> getGroupsByParam(Group group);

    List<Group> findReceptionGroupByMallCode(String mallCode);

    List<Group> findUnionGroupByMallCode(String mallCode);

    @Select("select * from t_group t where t.mall_code = #{mallCode}")
    public Group getGroupByMallCode(String mallCode);

    @Update(value = "update t_group set members=#{members} where id =#{id}")
    void updateGroupMembers(@Param("members") Integer members, @Param("id") Integer id);

    /**
     * 根据市场码查询联盟组
     */
    List<Group> findGroupByMallCode(@Param("mallCode") String mallCode);

    List<Group> findAllGroupByMallCode(@Param("mallCode") String mallCode);

    List<Group> findEnableUnionGroupByMallCode(@Param("mallCode") String mallCode);

    /**
     * XCXGY 2018/4/4
     */
    List<Group> findGroupListByMallCode(@Param("mallCode") String mallCode);

    /**
     * 根据市场码查询 对应蓄客卡的联盟组
     */
    List<Group> findStoredCardAndGroupByMallCode(@Param("mallCode") String mallCode);


//    List<Group> findGroupByMallCode(String mallCode);

    @Select(value = "select * from t_group where group_name =#{groupName}")
    Group getGroupByName(String groupName);

    @Select(value = "select * from t_group where group_name =#{groupName} and mall_code = #{mallCode} and not status = 2")
    Group getGroupByNameAndMallCode(@Param("groupName") String groupName, @Param("mallCode") String mallCode);


    @Select(value = "select * from t_group where type = #{type} and status = 0 and mall_code =#{mallCode} and not id =#{groupId}")
    List<Group> getGroupByTypeAndMallCode(@Param("type") Integer type, @Param("mallCode") String mallCode, @Param("groupId") Integer groupId);

    Group showGroupById();

    @Select(value = "select * from t_group where type=1 and mall_code=#{mallCode} AND `status` = 0 AND end_time > NOW() ")
    List<Group> queryGroup(String mallCode);

    List<StoredCard> getStoreCardByGroupId(Integer groupId);

    @Select(value = "select * from t_group where type =0 and is_home =1 and mall_code=#{mallCode} and status = 0")
    Group getGroupByIsHomeAndMallCode(String mallCode);

    @Update(value = "UPDATE t_group SET is_home = #{isHome} WHERE id=#{id}")
    void updateIsHomeById(@Param("id") Integer id, @Param("isHome") Integer isHome);

    @Select(value = "select t.group_name from t_group t where t.type = 1 and t.status = 0 order by convert(t.group_name using gbk) collate gbk_chinese_ci asc")
    List<String> findLMGroupNameList();

    @Update(value = "update t_group set is_home = 0 where mall_code = #{mallCode}")
    void setIsHome0ByMallCode(String mallCode);

    @Select(value = "select * from t_group as tg inner join t_group_fans as tgf on tg.id = tfg.group_id where tgf.employee_id = #{employeeId} ")
    List<Group> getGroupByEmployeeId(@Param("employeeId") Integer employeeId);

    @Select("select count(t.id) from t_group t where t.mall_code = #{mallCode} AND t.status = 0 AND t.type =0")
    Integer countByMallCode(@Param("mallCode") String mallCode);
}
